/* @Date 03 March 2008
 * @Author Hussein Patwa
 * Class for the main local trending algorithm (Fig 7 on KDD paper)
 */

import java.util.*;

public class local{
	HashSet posTrend = new HashSet(); // container for negative trends
	HashSet negTrend = new HashSet(); // container for positive trends
	HashSet paths;// = new HashSet();// = new HashSet(values); // set containing paths from values array (PROBLEM!)
	//HashSet obs = new HashSet(); // container for observations
	int minlength; // min length above which trend is considered established
	int maxlength; // max length above which trend is assumed
	
	ArrayList path; // an array to hold the spatialobjects comprising the path
	int corr; // something to hold the corrolation
        
	class observation{
            double dist; // distance from x co-ordinate 
            double diff; // distance from Y co-ordinate 
            observation(double dist,double diff){
                this.dist=dist;
                this.diff=diff;
            }
        }
        
        public local(NeighbourhoodInfo neighbourInfo,ArrayList data,int index,int minLength, int maxLength, double min_conf){
            paths = computeAllPaths(neighbourInfo,index,minLength);
            System.out.println("Number of Paths: "+paths.size());
            findTrends(index,data);
        }
        
        HashSet computeAllPaths(NeighbourhoodInfo neighbourInfo, int index,int minLength){
            //compute all the paths from the spatial object 
            //specified by the index using the neighbourhood information
            HashSet rtn = new HashSet();
            Vector tempNeighbours = neighbourInfo.getNeighbourhoodAreaIDs(index);
            System.out.println("index: "+index+" neighbours : "+tempNeighbours);
            for(Iterator i = tempNeighbours.iterator();i.hasNext();){
                Integer node = (Integer)i.next();
                ArrayList tempPath = new ArrayList(new Integer(index));
                tempPath.add(new Integer(node));
                rtn.add(tempPath);
                System.out.println(tempPath);
            }
            for(int j=0;j<minLength;j++)
                    rtn.addAll(extendPath(rtn,neighbourInfo));
            return rtn;
        }
        
        HashSet extendPath(HashSet paths, NeighbourhoodInfo neighbourInfo){
            HashSet rtn = new HashSet();
            for(Iterator k = paths.iterator();k.hasNext();){
                ArrayList path = (ArrayList)k.next();
                int index = ((Integer)path.get(path.size()-1)).intValue();
                Vector tempNeighbours = neighbourInfo.getNeighbourhoodAreaIDs(index);
                System.out.println("index: "+index+" neighbours : "+tempNeighbours);
                for(Iterator i = tempNeighbours.iterator();i.hasNext();){
                    Integer node = (Integer)i.next();
                    if(!path.contains(node)){
                        ArrayList tempPath = new ArrayList();
                        tempPath.addAll(path);
                        tempPath.add(node);
                        rtn.add(tempPath);
                        System.out.println(tempPath);
                    }
                }
            }
            return rtn;
        }
        
	public void findTrends(int index,ArrayList data){
            for(Iterator i = paths.iterator();i.hasNext();){
                path = (ArrayList)i.next(); // Move first element of paths to path
                HashSet PathObs = new HashSet();
                for(int j=minlength; j<path.size(); j++) // initialise counter that will track through the colums of the set paths
                {
                    System.out.println(path.size());
                    Integer currentIndex = (Integer)path.get(j);
                    spatialobject o = (spatialobject)data.get(index);
                    spatialobject object = (spatialobject)data.get(currentIndex);
                    double dist = Math.sqrt(Math.pow((object.getX()-o.getX()),2)+Math.pow((object.getY()-o.getY()),2)); // calculate the difference in the x co-ordinate
                    double diff = object.getV()-o.getV(); // calculate the difference in the y co-ordinate
                    observation obsTemp = new observation(dist,diff); // insert distX into observations
                    PathObs.add(obsTemp);// insert distY into observations
                    // Regression function
                    // Corrolation function
                    //System.out.println(obs);
                }
//                if(corr > 0) // Insert the path and corolation into the set posTrend or negTrend
//                {
//                    posTrend[0][0] = path;
//                    posTrend[0][1] = corr;
//                }
//                else
//                {
//                    negTrend[0][0] = path;
//                    negTrend[0][1] = corr;
//                }
                
            }
        }
	
		// Method to show pos or neg trend set 
	public void viewTrend(String trendDir){
	}
	

}
